package ace.module.captcha.sms.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.core.exception.BusinessException;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.manager.spring.AbstractSpringDtoManager;
import ace.cmp.i18n.api.MessageSourceAssertService;
import ace.module.captcha.sms.api.constant.CaptchaSmsErrorCodeConstant;
import ace.module.captcha.sms.api.model.dto.CaptchaSmsItemConfigDto;
import ace.module.captcha.sms.core.impl.dao.entity.CaptchaSmsItemConfig;
import ace.module.captcha.sms.core.impl.manager.CaptchaSmsItemConfigManager;
import ace.module.captcha.sms.core.impl.manager.cache.CaptchaSmsItemConfigCacheDbManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
* 短信验证码配置 spring data jdbc querydsl rsql manager implement.
*
* @author Caspar
*/
@AllArgsConstructor
@Component
@Slf4j
public class CaptchaSmsItemConfigManagerImpl
    extends AbstractSpringDtoManager<CaptchaSmsItemConfig, Long, CaptchaSmsItemConfigDto>
    implements CaptchaSmsItemConfigManager {
  private final CaptchaSmsItemConfigCacheDbManager captchaSmsItemConfigCacheDbManager;
  private final MessageSourceAssertService assertService;

  @Override
  public CaptchaSmsItemConfig getAndCheckAllById(Long id) {
    CaptchaSmsItemConfig config = captchaSmsItemConfigCacheDbManager.getById(id);

    assertService.isNull(
        config,
        CaptchaSmsItemConfig.class.getName() + ".is-null",
        new Object[] {id},
        "[{0}]CaptchaSmsItemConfig,数据不存在",
        msg -> {
          throw new BusinessException(
              CaptchaSmsErrorCodeConstant.CAPTCHA_SMS_VERITY_CODE_NO_CONFIG, msg);
        });

    assertService.isFalse(
        CoreConstant.DELETE_FLAG_NOT.equals(config.getDeleteFlag()),
        CaptchaSmsItemConfig.class.getName() + ".is-deleted",
        new Object[] {id},
        "[{0}]CaptchaSmsItemConfig,数据不存在",
        msg -> {
          throw new BusinessException(
              CaptchaSmsErrorCodeConstant.CAPTCHA_SMS_VERITY_CODE_NO_CONFIG, msg);
        });

    return config;
  }

  @Override
  public CaptchaSmsItemConfig getById(Long id) {
    return this.captchaSmsItemConfigCacheDbManager.getById(id);
  }
}
